package com.snj.bestdealer.lib;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

/**
 * GraphicsUtil an utility class which convert the 
 * image in circular shape
 * Author : Mukesh Yadav, Kei
 */
public class GraphicsUtil {
	String TAG = "GraphicsUtil";
	/*
	 * Draw image in circular shape
	 * Note: change the pixel size if you want image small or
	 * large
	 */
	public Bitmap getCircleBitmap(Bitmap srcBitmap, int pixels) {
		Bitmap output = null, cropBmp;
		Canvas canvas;
		try {
			if(srcBitmap.getWidth() >= srcBitmap.getHeight()) {
				cropBmp = Bitmap.createBitmap(
						srcBitmap, 
						srcBitmap.getWidth()/2 - srcBitmap.getHeight()/2, 
						0, 
						srcBitmap.getHeight(), 
						srcBitmap.getHeight());
			}else{
				cropBmp = Bitmap.createBitmap(
						srcBitmap, 
						0, 
						srcBitmap.getHeight()/2 - srcBitmap.getWidth()/2, 
						srcBitmap.getWidth(), 
						srcBitmap.getWidth());
			}
			
			output = Bitmap.createBitmap(cropBmp.getWidth(), cropBmp.getHeight(), Bitmap.Config.ARGB_8888);
			canvas = new Canvas(output);
			
			final int color = 0xffff0000;
			final Paint paint = new Paint();
			final Rect rect = new Rect(0, 0, cropBmp.getWidth(), cropBmp.getHeight());
			final RectF rectF = new RectF(rect);

			paint.setAntiAlias(true);
			paint.setDither(true);
			paint.setFilterBitmap(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawOval(rectF, paint);

			paint.setColor(Color.BLUE);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth((float) 4);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(cropBmp, rect, rect, paint);
		} catch (OutOfMemoryError e) {
			// TODO: handle exception
			Log.i(TAG, "exception error : " + e.getMessage());
			//System.out.println(e.getMessage());
		}
		return output;
	}

}
